package com.hunyuan.player;

import android.support.annotation.NonNull;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hunyuan on 2017/1/17.
 */

public class MusicFactory {

    private static List<Music> musicList;


    public static List<Music> buildMusic() {
        if (musicList == null) {
            musicList = new ArrayList<>();
            musicList.add(new Music("1", "幻听", "http://m10.music.126.net/20170124134257/d42018b020e7b69d1cce1d022ada3b09/ymusic/892d/6050/f60d/2ac5daca7488fe23caaabe9e78c84883.mp3"));
            musicList.add(new Music("2", "No.284 能救人的迷幻小蘑菇", "http://media.blubrry.com/142999/fdfs.xmcdn.com/group22/M0B/23/8B/wKgJLlhq7quSU71vATV9sSyHuk4143.mp3"));
            musicList.add(new Music("3", "No.282 圣诞树到底是什么树", "http://media.blubrry.com/142999/fdfs.xmcdn.com/group25/M07/CE/C1/wKgJMVhVO4TBN7RkAQx-Lh3etmQ061.mp3"));
        }
        return musicList;
    }

    public static List<MediaSessionCompat.QueueItem> getMusicQueue() {
        return convertToQueue(getMusicList());
    }

    private static List<MediaSessionCompat.QueueItem> convertToQueue(List<MediaMetadataCompat> musicList) {
        List<MediaSessionCompat.QueueItem> queue = new ArrayList<>();
        int count = 0;
        for (MediaMetadataCompat music : musicList) {
            queue.add(new MediaSessionCompat.QueueItem(music.getDescription(), count++));
        }
        return queue;
    }

    public static List<MediaMetadataCompat> getMusicList() {
        List<MediaMetadataCompat> mediaMetadataCompatList = new ArrayList<>();
        for (Music music : buildMusic()) {
            mediaMetadataCompatList.add(buildMediaMetadata(music));
        }
        return mediaMetadataCompatList;
    }

    public static MediaMetadataCompat getMediaMetadataById(@NonNull String musicId) {
        MediaMetadataCompat metadata = null;
        for (Music music : buildMusic()) {
            if (musicId.equals(music.getId()))
                metadata = buildMediaMetadata(music);
        }
        return metadata;
    }

    public static Music getMusicById(@NonNull String musicId) {
        for (Music music : buildMusic()) {
            if (musicId.equals(music.getId()))
                return music;
        }
        return null;
    }

    private static MediaMetadataCompat buildMediaMetadata(Music music) {
        return new MediaMetadataCompat.Builder()
                .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, music.getId())
                .putString(MediaMetadataCompat.METADATA_KEY_TITLE, music.getTitle())
                .putString(MediaMetadataCompat.METADATA_KEY_ART_URI, music.getUrl())
                .build();
    }
}
